package com.seven.monior.TaskInfo;

import java.util.ArrayList;
import java.util.List;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.seven.monior.AppInfo.Global;

public class TaskInfo {
	private int m_nUID;
	private int m_nPID;
	private boolean m_bIsRunning;

	private boolean m_bIsSystemApp = true;

	public Long m_lMobile_Rx = 0L;

	public Long m_lMobile_Tx = 0L;
	public Long m_lOther_Rx = 0L;
	public Long m_lOther_Tx = 0L;
	public Long m_lTotal_Rx = 0L;
	public Long m_lTotal_Tx = 0L;
	public Long m_lMobile_last_Rx = 0L;
	public Long m_lMobile_last_Tx = 0L;
	public Long m_lOther_last_Rx = 0L;
	public Long m_lOther_last_Tx = 0L;
	public Long m_lTotal_last_Rx = 0L;
	public Long m_lTotal_last_Tx = 0L;
	private Drawable m_Icon = null;

	private String m_sProcessName = "";
	private String m_sApplicationLabel = "";
	private ArrayList<String> m_sApplicationLabelList = new ArrayList<String>();
	private String m_sPkgName = "";
	private ArrayList<String> m_sPkgNameList = new ArrayList<String>();

	private double m_lastCpuTime = 0L;
	private double m_totalCpuTime = 0L;
	// private long totalMemoryUsage = 0L;
	public double cpuStime;
	public double cpuUtime;

	public double memory;

	private PackageManager pm = Global.pm;

	public TaskInfo() {

	}

	public TaskInfo(int m_nUID, boolean m_bIsRunning, Long m_lMobile_Rx,
			Long m_lMobile_Tx, Long m_lTotal_Rx, Long m_lTotal_Tx) {
		super();
		this.m_nUID = m_nUID;
		this.m_nPID = -1;
		this.m_bIsRunning = m_bIsRunning;
		this.m_lMobile_Rx = m_lMobile_Rx;
		this.m_lMobile_Tx = m_lMobile_Tx;
		this.m_lTotal_Rx = m_lTotal_Rx;
		this.m_lTotal_Tx = m_lTotal_Tx;
		setInfo(m_nUID, pm);
	}

	public TaskInfo(int m_nUID, boolean m_bIsRunning, Long m_lMobile_Rx,
			Long m_lMobile_last_Rx, Long m_lMobile_Tx, Long m_lMobile_last_Tx,
			Long m_lOther_Rx, Long m_lOther_last_Rx, Long m_lOther_Tx,
			Long m_lOther_last_Tx, Long m_lTotal_Rx, Long m_lTotal_last_Rx,
			Long m_lTotal_Tx, Long m_lTotal_last_Tx) {
		super();
		this.m_nUID = m_nUID;
		this.m_bIsRunning = m_bIsRunning;
		this.m_lMobile_Rx = m_lMobile_Rx;
		this.m_lMobile_last_Rx = m_lMobile_last_Rx;
		this.m_lMobile_Tx = m_lMobile_Tx;
		this.m_lMobile_last_Tx = m_lMobile_last_Tx;
		this.m_lOther_Rx = m_lOther_Rx;
		this.m_lOther_last_Rx = m_lOther_last_Rx;
		this.m_lOther_Tx = m_lOther_Tx;
		this.m_lOther_last_Tx = m_lOther_last_Tx;
		this.m_lTotal_Rx = m_lTotal_Rx;
		this.m_lTotal_last_Rx = m_lTotal_last_Rx;
		this.m_lTotal_Tx = m_lTotal_Tx;
		this.m_lTotal_last_Tx = m_lTotal_last_Tx;
		setInfo(m_nUID, pm);
	}

	public Drawable getIcon() {
		return m_Icon;
	}

	public int getUID() {
		return m_nUID;
	}

	public int getPID() {
		return m_nPID;
	}

	public void setPID(int m_nPID) {
		this.m_nPID = m_nPID;
	}

	public boolean getIsRunning() {
		return m_bIsRunning;
	}

	public void setIsRunning(boolean isRunning) {
		m_bIsRunning = isRunning;
	}

	public Long getMobile_Rx() {
		return m_lMobile_Rx;
	}

	public Long getMobile_Tx() {
		return m_lMobile_Tx;
	}

	public Long getMobile_RxandTx() {
		return m_lMobile_Rx + m_lMobile_Tx;
	}

	public Long getTotal_Rx() {
		return m_lTotal_Rx;
	}

	public Long getTotal_Tx() {
		return m_lTotal_Tx;
	}

	public Long getTotal_RxandTx() {
		return m_lTotal_Rx + m_lTotal_Tx;
	}

	public String getProcessName() {
		return m_sProcessName;
	}

	public String getApplicationLabel() {
		return m_sApplicationLabel;
	}

	public ArrayList<String> getApplicationLabelList() {
		return m_sApplicationLabelList;
	}

	private void setInfo(int nUID, PackageManager pm) {
		List<PackageInfo> packageInfos = pm.getInstalledPackages(0);
		for (int i = 0; i < packageInfos.size(); i++) {
			PackageInfo pInfo = packageInfos.get(i);
			if (pInfo.applicationInfo.uid == nUID) {
				if ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0
						|| (pInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
					m_bIsSystemApp = false;
				}
				m_Icon = pInfo.applicationInfo.loadIcon(pm);
				m_sProcessName = pInfo.applicationInfo.processName;
				String sLabel = pInfo.applicationInfo.loadLabel(pm).toString();
				if (!sLabel.equals("null")) {
					m_sApplicationLabel = sLabel;
					m_sApplicationLabelList.add(sLabel);
				}

				String sPkgName = pInfo.packageName;
				m_sPkgName = sPkgName;
				m_sPkgNameList.add(sPkgName);
			}
		}

		if (nUID == 1000) {
			m_sApplicationLabel = "Android System";
		} else if (nUID == 1001) {
			m_sApplicationLabel = "Dialer";
		} else {
			if (m_sApplicationLabelList.size() > 1) {
				for (String label : m_sApplicationLabelList) {
					if (label.contains("Google Services Framework")
							|| label.contains("Download Manager")
							|| label.contains("Home")) {
						m_sApplicationLabel = label;
						break;
					}
				}
			}
		}
	}

	public String getPkgName() {
		return m_sPkgName;
	}

	public ArrayList<String> getPkgNameList() {
		return m_sPkgNameList;
	}

	public String getTaskInfo() {
		String info = "";
		info += "UID: " + m_nUID + "\n";
		info += "Is_Runnning: " + (m_bIsRunning ? "Yes" : "No") + "\n";
		info += "Mobile_Rx: " + m_lMobile_Rx + "\n";
		info += "Mobile_Tx: " + m_lMobile_Tx + "\n";
		info += "Other_Rx: " + m_lOther_Rx + "\n";
		info += "Other_Tx: " + m_lOther_Tx + "\n";
		info += "Total_Rx: " + m_lTotal_Rx + "\n";
		info += "Total_Tx: " + m_lTotal_Tx + "\n";
		info += "Mobile_last_Rx: " + m_lMobile_last_Rx + "\n";
		info += "Mobile_last_Tx: " + m_lMobile_last_Tx + "\n";
		info += "Other_last_Rx: " + m_lOther_last_Rx + "\n";
		info += "Other_last_Tx: " + m_lOther_last_Tx + "\n";
		info += "Total_last_Rx: " + m_lTotal_last_Rx + "\n";
		info += "Total_last_Tx: " + m_lTotal_last_Tx + "\n";

		return info;

	}

	public void setPkgList(String[] pkgList) {
		for (String pkg : pkgList) {
			m_sPkgNameList.add(pkg);
		}
	}

	public double getTotalCpuTime() {
		return m_totalCpuTime;
	}

	public void setTotalCpuTime(double cpuTime) {
		m_totalCpuTime = cpuTime;
	}

	public double getlastCpuTime() {
		return m_lastCpuTime;
	}

	public void setlastCpuTime(double m_lastCpuTime) {
		this.m_lastCpuTime = m_lastCpuTime;
	}

	public boolean isSystemApp() {
		return m_bIsSystemApp;
	}

	public double getMemory() {
		return memory;
	}

	public void setMemory(double memory) {
		this.memory = memory;
	}

}
